<?php class inventory extends Model {

    function __construct()
    {
        parent::__construct();
    }
    
    public function join_select($cond=""){
    
    	$this->db->select('category.level as level,inventory.count as r_count,category.codes as codes, inventory.mod_id as mod_id,inventory.category_id as category_id,inventory.version_id as version_id,models.name AS mod_name, category.name AS category_name,category.codes AS codes, version.name AS version_name,inventory.receivetime as receivetime,inventory.receive_code as r_code');
    	$this->db->from('inventory');
    	$this->db->join('models','models.mod_id = inventory.mod_id','left');
    	$this->db->join('category','category.category_id = inventory.category_id','left');
    	$this->db->join('version','version.version_id = inventory.version_id','left');
    	if($cond!=""){
    		$this->db->where($cond);
    	}
    	$this->db->order_by('category.codes','asc');
    	$query = $this->db->get();
    	$result = $query->result();
    	//echo $this->db->last_query();exit;
    	return $result;
    }
    
    
    
    function truncate(){
    	return $this->db->truncate('inventory');
    }
    function insert($data){
       $result = $this->db->insert('inventory',$data);
       return $result;
    }
    function insert_for_excel($array){

       $re = $this->select_by_array(array('mod_id'=>$array['mod_id'],'category_id'=>$array['category_id'],'version_id'=>$array['version_id']));
       if(count($re)==0){
         $this->db->insert('inventory',$array);
         $result = $this->db->insert_id();
       }else{
         $count = $re[0]->count+$array['count'];
         $this->update($re[0]->inventory_id,array('count'=>$count));
         $result = $re[0]->inventory_id;
       }
       return $result;
    }
    function select_join($ar){
       $this->db->select('*');
       $this->db->where($ar);
       $this->db->where('count !=',0);
	   $this->db->from('inventory');
	   $this->db->join('category', 'inventory.category_id = category.category_id');
       $query = $this->db->get();
       $result = $query->result(); 
       return $result;
    
    }
    
    function getlast($version_id,$id){
       $this->db->where(array('category_id'=>$id,'version_id'=>$version_id));
       $this->db->select_sum('count');
       $query = $this->db->get('inventory');
       $result = $query->result(); 
       return $result;
    }
    
    
    function select_by_id($inventory_id){
       $this->db->where('inventory_id',$inventory_id);
       $query = $this->db->get('inventory');
       $result = $query->result(); 
       return $result;
    }
    function getlast_fo($version_id,$id){

       $this->db->where('category_id',$id);
       $this->db->where('version_id !=',$version_id);
       $this->db->select_sum('count');
       $query = $this->db->get('inventory');
       $result = $query->result(); 
       return $result;
    }
    
    function getlast_foo($version_id,$id){

       $this->db->where('category_id',$id);
       $this->db->where('version_id !=',$version_id);
       $this->db->where('count !=',0);
       $query = $this->db->get('inventory');
       $result = $query->result(); 
       return $result;
    }
    
    
    function getcount($id,$version_id=''){
    	$where['category_id']=$id;
    	if($version_id!=""){
    	 $where['version_id']=$version_id;
    	}
        $this->db->where($where);
    	$this->db->select_sum('count');
        $query = $this->db->get('inventory');
        $result = $query->result(); 
        return $result;
    }
    
    function select_count($category_id){
       $this->db->select_sum('count');
       $this->db->where('category_id',$category_id);
       $query = $this->db->get('inventory');       
       $result = $query->result(); 

       return $result;    
    }
    
    function select_total_for_caculate($array){
    	$this->db->select_sum('count');
    	$this->db->where($array);
    	$query = $this->db->get('inventory');
    	$result = $query->result();
    	
    	return $result;
    }
    
    function select_where($category_id,$version_id){
       $this->db->select_sum('count');
       $this->db->where('category_id',$category_id);
       $this->db->where('version_id !=',$version_id);
       $query = $this->db->get('inventory');
       
       $result = $query->result(); 

       return $result[0]->count;
    }
    
    
    function  select_by_array($array){
       $this->db->where($array); 	
       $query = $this->db->get('inventory');       
       $result = $query->result(); 
     //  echo $this->db->last_query();
       return $result;
    }
    
    
    function select($array,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($array!=""){
       	  $this->db->where($array);
       }
       $this->db->where('count !=',0);
       $this->db->order_by('inventory_id desc');
       $query = $this->db->get('inventory');
       
       $result = $query->result(); 
       return $result;
    }
    
    function select3($array,$num="",$offset=""){
       $this->db->select("*");
       $this->db->from("inventory");
       $this->db->join('category', 'inventory.category_id = category.category_id');
       $this->db->limit($num,$offset);	
       if($array!=""){
       	  $this->db->where($array);
       }
       $this->db->where('count !=',0);
       if(!isset($array['inventory.category_id'])){
          $this->db->order_by('category.codes asc');
       }
       $query = $this->db->get();
       
       $result = $query->result(); 
       return $result;
    }
    
    function select_distinct_mod(){
    
    	$sql = "select DISTINCT mod_id from inventory ";
    	
    	$query = $this->db->query($sql);
    	$result = $query->result();
    	return $result;
    }
    
    function select_distinct_category(){
    	$sql = "select DISTINCT category_id from inventory ";
    	 
    	$query = $this->db->query($sql);
    	$result = $query->result();
    	return $result;
    }
    
    function select_distinct_count(){
    	$this->db->select_sum('count');
    	$query = $this->db->get('inventory');
    	$result = $query->result();
    	 
    	return $result;
    }
    
    function select2($array,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($array!=""){
       	  $this->db->where($array);
       }
       $this->db->order_by('inventory_id desc');
       $query = $this->db->get('inventory');
       
       $result = $query->result(); 
       return $result;
    }

    
    function update($id,$array){
       $result = $this->db->update('inventory',$array,array('inventory_id'=>$id));
       return $result;
    }
    
    function del($id){
       $result = $this->db->delete('inventory',array('inventory_id'=>$id));
       return $result;
    }
}